<template>
  <div class="login-container">
    <el-card class="box-card">
      <!-- 标题 -->
      <div class="title"></div>
      <el-form ref="form" :model="user" :rules="rules">
        <el-form-item prop="username">
          <!-- 用户名 -->
          <el-input prefix-icon="el-icon-s-custom" placeholder="请输入用户名" v-model="user.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!-- 密码 -->
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
            type="password"
            show-password
            v-model="user.password"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <!-- 确认密码 -->
          <el-input
            prefix-icon="el-icon-lock"
            placeholder="请确认密码"
            type="password"
            show-password
            v-model="user.repassword"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <!-- 按钮 -->
          <el-button type="primary" @click="reg">注册</el-button></el-form-item
        >
      </el-form>
      <el-link :underline="false" @click="$router.push('/login')">已有账号,去登录</el-link>
    </el-card>
  </div>
</template>
<script>
import { RegAPI } from '@/api/user'
export default {
  name: 'MyReg',
  data() {
    // 确认密码验证
    const validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'))
      } else if (value !== this.user.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      user: { username: '', password: '', repassword: '' },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,10}$/, message: '长度在 1 到 10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 15, message: '长度在 6 到 15 个字符', trigger: 'blur' }
        ],
        repassword: [{ validator: validatePass2, trigger: 'blur' }]
      }
    }
  },
  methods: {
    reg() {
      // form 是 <el-form> 的ref属性值，必须有这个值
      this.$refs.form.validate(async valid => {
        // console.log(valid) // 验证通过valid=true;  验证不通过valid=false
        if (!valid) return // 验证不通过，则return
        // console.log('验证通过，可以提供数据了')

        const { data: res } = await RegAPI(this.user)
        if (res.code === 0) {
          // 注册成功：提示、重置表单、跳转到登录页
          this.$message.success(res.message)
          this.$refs.form.resetFields() // 重置表单
          this.$router.push('/login') // 跳转到登录页
        } else {
          this.$message.error(res.message)
        }
      })
    }
  }
}
</script>
<style>
.login-container {
  background: url('@/assets/images/login_bg.jpg') no-repeat;
  background-size: cover;
  height: 100%;
}
.el-card {
  width: 400px;
  /* height: 310px; */
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.title {
  height: 40px;
  background: url(@/assets/images/login_title.png) top center no-repeat;
}
.el-button {
  width: 100%;
}
</style>
